# reminder
#2 prefix !  create a new window from a pane
# prefix z  maximux the pane
# prefix s show all sessions
# prefix ( previous session
# prefix ) next session
# prefix . move window from one session to another

# Set the default terminal mode to 256color mode
set -g default-terminal "screen-256color"
set-option -sa terminal-overrides ',screen-256color:RGB'
set -g history-limit 10000
# enable vi keys.
# prefix + [ enter vi mode
setw -g mode-keys vi
set-option -g status-keys vi
# Setting the prefix from C-b to C-a
set -g prefix C-a
# set -g prefix `
bind-key a last-window
bind-key e send-prefix

set-option -g renumber-windows on

# Ensure that we can send Ctrl-A to other apps
bind C-a send-prefix

#setting the delay between prefix and command
set -sg escape-time 10

# Set the base index for windows to 1 instead of 0
set -g base-index 1

# Set the base index for panes to 1 instead of 0
setw -g pane-base-index 1

# edit and reload tmux configuration quickly
bind r source-file ~/.config/tmux/tmux.conf \; display-message "~/.config/tmux/tmux.conf reloaded"

# splitting panes with | and -
# vim-tmux-navigator use current mapping
# bind \ split-window -h
bind _ split-window -v
# split pane and retain the current directory of existing pane
bind - split-window -v -c "#{pane_current_path}"
bind |  split-window -h -c "#{pane_current_path}"

# moving between panes with Prefix h,j,k,l
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R


# Quick window selection
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+

# resize pane
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

set -g mouse on
bind-key m set-option -g mouse \; display "Mouse: #{?mouse,ON,OFF}"
# Sane scrolling
set -g terminal-overrides 'xterm*:smcup@:rmcup@'
set -g focus-events on

# create session
bind C-c new-session
# find session
bind C-f command-prompt -p find-session 'switch-client -t %%'

bind l last-window

# switch to last-viewed session
bind L switch-client -l

# fix mouse wheel behavior
# bind-key -T copy-mode-vi WheelUpPane send-keys -X scroll-up
# bind-key -T copy-mode-vi WheelDownPane send-keys -X scroll-down


bind -T copy-mode-vi 'space' send-keys -X halfpage-up
bind -T copy-mode-vi a send-keys -X halfpage-up
bind -T copy-mode-vi s send-keys -X halfpage-down
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi C-v send-keys -X rectangle-toggle
bind -T copy-mode-vi Y send-keys -X copy-end-of-line
bind -T copy-mode-vi y send-keys -X copy-selection-and-cancel
# bind -T copy-mode-vi = send-keys -X search-again
# bind -T copy-mode-vi = send-keys -X search-reverse



# -- toggle_syn_input
bind C-g if-shell '[[ $(tmux show synchronize-panes | cut -d\  -f2) == "on" ]]' \
'setw synchronize-panes off; set -g pane-border-style fg=magenta' \
'setw synchronize-panes on; set -g pane-border-style fg=red'


# set colors for the active window
# setw -g window-status-current-style fg=white,bold,bg=red

# colors for pane borders
setw -g pane-border-style fg=green,bg=black
setw -g pane-active-border-style fg=white,bg=yellow

# active pane normal, other shaded out
 # setw -g window-style fg=colour240,bg=colour235
 # setw -g window-active-style fg=white,bg=black

# shortcut for synchronize-panes toggle
# execute commands in multiple panes simultaneously
bind C-s set-window-option synchronize-panes

bind -n Pageup copy-mode -u
bind -n S-Pageup copy-mode -u
bind -n S-Pagedown send-keys Pagedown


# mapping <prefix> C-l to clear the screen
bind C-l send-keys 'C-l'
bind-key m set-option -g mouse \; display "Mouse: #{?mouse,ON,OFF}"


# bind -T copy-mode    DoubleClick1Pane select-pane \; send -X select-word \; send -X copy-pipe-no-clear "xsel -i"
# bind -T copy-mode-vi DoubleClick1Pane select-pane \; send -X select-word \; send -X copy-pipe-no-clear "xsel -i"
# bind -n DoubleClick1Pane select-pane \; copy-mode -M \; send -X select-word \; send -X copy-pipe-no-clear "xsel -i"
# bind -T copy-mode    TripleClick1Pane select-pane \; send -X select-line \; send -X copy-pipe-no-clear "xsel -i"
# bind -T copy-mode-vi TripleClick1Pane select-pane \; send -X select-line \; send -X copy-pipe-no-clear "xsel -i"
# bind -n TripleClick1Pane select-pane \; copy-mode -M \; send -X select-line \; send -X copy-pipe-no-clear "xsel -i"
# bind -n MouseDown2Pane run "tmux set-buffer -b primary_selection \"$(xsel -o)\"; tmux paste-buffer -b primary_selection; tmux delete-buffer -b primary_selection"
# bind -T copy-mode    C-c send -X copy-pipe-and-cancel "xsel -i --clipboard"
# bind -T copy-mode-vi C-c send -X copy-pipe-and-cancel "xsel -i --clipboard"

# left click copy content
# bind -T copy-mode    MouseDown1Pane send -X copy-pipe-and-cancel "xsel -i --clipboard"
# bind -T copy-mode-vi MouseDown1Pane send -X copy-pipe-and-cancel "xsel -i --clipboard"

# selection end copy
bind -T copy-mode MouseDragEnd1Pane send-keys -X copy-selection-no-clear
bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-selection-no-clear

bind -n C-S-v run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"

# paste from right click
unbind-key MouseDown3Pane
bind-key -n MouseDown3Pane run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"
# run " \
#   X=$(xclip -o -sel clipboard); \
#   tmux set-buffer \"$X\"; \
#   tmux paste-buffer -p; \
# "

## Clipboard integration
# ctrl+c to send to clipboard
# bind C-c run "tmux save-buffer - | xclip -i -sel clipboard"

# ---------------------
set-hook -g pane-focus-in "run -b 'bash ~/.config/tmux/fzf_panes.tmux update_mru_pane_ids'"
bind -n M-w run -b 'bash ~/.config/tmux/fzf_panes.tmux new_window'

# plugins


# start of tpm's plugins 
# set -g @plugin 'tmux-plugins/tmux-yank'
# set -g @plugin 'tmux-plugins/tmux-copycat'
# set -g @yank_action 'copy-pipe'
set -g @plugin 'christoomey/vim-tmux-navigator'
set -g @plugin 'laktak/extrakto'

# set -g @plugin 'ofirgall/tmux-window-name'
set -g @tmux_window_dir_programs "['git']"


# tmux's theme
set -g @plugin 'dracula/tmux'
set -g @dracula-show-flags true
set -g @dracula-show-left-icon session
set -g @dracula-plugins "ram-usage"
set -g @dracula-show-powerline true
set -g @dracula-border-contrast true
set -g @dracula-network-bandwidth-colors "pink dark_gray"

# prefix + spc trigger
set -g @plugin 'fcsonline/tmux-thumbs'
set -g @thumbs-command 'echo -n {} | xclip -sel clip'

set -g @plugin 'yyancy/tmux-tilish'
set -g @tilish-navigator 'on'

run '~/.config/tmux/plugins/tpm/tpm'

